<template>
    <div class="order-report">
        <div class="report-table-top-title">总店管理</div>
        <div class="report-table-form1">
            <el-form
                ref="form"
                size="small"
                inline
                :model="form"
                label-width="80px"
            >
                <el-form-item
                    label="代理商:"
                    v-if="roleId === 1"
                    prop="agentId"
                >
                    <el-select
                        class="item"
                        v-model="form.agentId"
                        filterable
                        clearable
                        placeholder="请选择代理商"
                        size="small"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="item in agentList"
                            :key="item.id"
                            :label="item.agentName"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="总店编码:" prop="companyCode">
                    <el-input
                        type="text"
                        v-model="form.companyCode"
                        class="item"
                    ></el-input>
                </el-form-item>
                <el-form-item label="总店名称:" prop="companyName">
                    <el-input
                        type="text"
                        v-model="form.companyName"
                        class="item"
                    ></el-input>
                    <el-button
                        type="primary"
                        size="small"
                        @click="
                            position = 1
                            getList()
                        "
                        >查询</el-button
                    >
                    <el-button type="primary" size="small" @click="resetFields"
                        >重置</el-button
                    >
                    <el-button
                        type="primary"
                        size="small"
                        @click="
                            showAdd = true
                            currRow = {}
                        "
                        >添加</el-button
                    >
                     <el-button type="primary" size='small' @click="showSync=true">同步商家数据</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList"
                border
                style="width: 100%"
            >
                <el-table-column
                    prop="companyCode"
                    label="总店编码"
                ></el-table-column>
                <el-table-column
                    prop="companyName"
                    label="总店名称"
                ></el-table-column>
                <el-table-column
                    prop="telPhone"
                    label="联系电话"
                ></el-table-column>
                <el-table-column
                    prop="agentName"
                    label="代理商"
                ></el-table-column>
                <el-table-column
                    prop="cityLevelCode"
                    label="行政编号"
                ></el-table-column>
                <el-table-column
                    prop="address"
                    label="详细地址"
                ></el-table-column>

                <el-table-column
                    prop="statusName"
                    label="状态"
                ></el-table-column>
                <el-table-column label="操作" fixed="right" width="250">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="primary"
                            @click="toDetail(scope.row)"
                            >编辑</el-button
                        >
                        <el-button
                            size="mini"
                            plain
                            type="primary"
                            v-if="scope.row.status === 0"
                            @click="link(scope.row)"
                            >设置连锁</el-button
                        >
                        <el-button
                            size="mini"
                            plain
                            type="primary"
                            v-if="roleId === 1 && scope.row.status === 1"
                            @click="updateCompanyStatus(scope.row)"
                            >快速启用</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="report-table-block">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="10"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>
        <div class="page-padding"></div>
        <setAgent
            :value.sync="showValue"
            :currRow="currRow"
            @getList="getList"
        ></setAgent>
        <agentSetList
            :value.sync="showAgentValue"
            :currRow="currRow"
            @getList="getList"
        ></agentSetList>
        <setShop
            :value.sync="showAdd"
            :currRow="currRow"
            @getList="getList"
        ></setShop>
        <synchronousShop
            :value.sync="showSync"
            :currRow="currRow"
            @getList="getList"
        ></synchronousShop>
    </div>
</template>

<script type="text/ecmascript-6">
import setAgent from './components/setAgent'
import agentSetList from './components/agentSetList'
import setShop from './components/setShop'
import synchronousShop from '../page_1_KTV_manage/components/synchronousShop'
export default {
    components: {
        setAgent,
        setShop,
        agentSetList,
        synchronousShop
    },
    data: function () {
        return {
            agentList: [], // 代理商
            currRow: {}, // 当前行
            showAgentValue: false,
            showSync: false,
            showAdd: false,
            showValue: false,
            totalNum: 0,
            position: 1,
            form: {
                companyCode: '',
                companyName: '',
                agentId: ''
            },
            showList: []
        }
    },
    mounted () {
    },
    created () {
        this.getAllAgent()
        this.getList()
    },
    watch: {
        position () {
            this.getList()
        }
    },
    computed: {
        roleId () {
            return this.$store.state.roleId
        }
    },
    methods: {
        async updateCompanyStatus (row) {
            let res = await this.$http.post(this.$store.state.IP + '/headCompany/updateCompanyStatus', {
                status: 0,
                companyCode: row.companyCode,
                companyId: row.companyId
            })
            let response = res.data
            if (response.ret === '0') {
                this.$message.success('设置成功')
                this.getList()
            }
        },
        async getAllAgent () {
            let res = await this.$http.get(this.$store.state.IP + '/systemUserManage/getAgents', {
                data: {}
            })
            let response = res.data
            if (response.ret === '0') {
                console.log(res)
                this.agentList = response.res
            }
        },
        toDetail (row) {
            this.currRow = row
            this.showAdd = true
        },
        link (row) {
            this.currRow = row
            if (this.roleId === 1) {
                this.showValue = true
            }
            if (this.roleId === 2) {
                this.showAgentValue = true
            }
        },
        resetFields () {
            this.$refs['form'].resetFields()
            this.position = 1
            this.getList()
        },
        // 换页方法
        handleCurrentChange (val) {
            this.position = val
        },
        async getList () {
            let res = await this.$http.post(this.$store.state.IP + '/headCompany/page', {
                ...this.form,
                page: this.position - 1, // 页数
                size: 10 // 一页显示数量
            })
            let response = res.data
            if (response.ret === '0') {
                console.log(res)
                this.showList = response.data.list
                this.totalNum = response.data.total
            }
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.item
    width: 200px

.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-form1
        width: 100%
        min-height: 40px
        padding-top: 15px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align: left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff
</style>
